package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CustomerBillAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseOriginal;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.BaseParams;
import com.isunland.managesystem.entity.CustomerMainOriginal;
import com.isunland.managesystem.entity.CustomerMainOriginal.rCustomerMain;
import com.isunland.managesystem.entity.rContractListMain;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/7/5 16:27
 */
public class CustomerBillListFragment extends BaseListFragment {
  private ArrayList<rContractListMain> mList;
  private CustomerBillAdapter mAdapter;
  private rCustomerMain mItem;

  @Override public void initData() {
    super.initData();
    mItem = (!(mBaseParams.getItem() instanceof CustomerMainOriginal.rCustomerMain))
        ? new CustomerMainOriginal().new rCustomerMain() : (rCustomerMain) mBaseParams.getItem();
    mList = new ArrayList<>();
    mAdapter = new CustomerBillAdapter(mActivity, mList);
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setListAdapter(mAdapter);
    setTitleCustom(R.string.customerBill);
  }

  @Override public String getUrl() {
    return ApiConst.URL_QUERY_LIST_CUSTOMER_BILL;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("contractProperty", "in");//合同性质
    params.put("dataStatus", "checkPass");
    params.put("isPaging", "0");//不分页
    params.put("memberCode", mCurrentUser.getMemberCode());
    params.put("minkindCode", "undefined");//采购订单类别编码
    params.put("moutkindCode",
        MyStringUtil.connect(mCurrentUser.getMemberCode(), "-DJLB-OUT-XSDDCK"));//采购订单类别编码
    params.put("orderField", "REG_DATE");
    params.put("orderSeq", "desc");
    params.put("partId", mItem.getId());
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    BaseOriginal<rContractListMain> result =
        new Gson().fromJson(json, new TypeToken<BaseOriginal<rContractListMain>>() {
        }.getType());
    mList.clear();
    mList.addAll(result.getRows());
    mAdapter.notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    if (position < mListview.getHeaderViewsCount()) return;
    rContractListMain item = mAdapter.getItem(position - l.getHeaderViewsCount());
    if (item == null) return;
    BaseVolleyActivity.newInstance(this, CustomerBillDetailActivity.class,
        new BaseParams<rContractListMain>().setItem(item), 0);
  }
}
